﻿/*************************************************
作者: Plane
邮箱: 1785275942@qq.com
功能: 游戏根节点

           ~~获取更多教学资讯~~
//----------------*----------------\\
        教学官网：www.qiqiker.com
        官方微信服务号: qiqikertuts
        Plane老师微信: PlaneZhong
\\----------------*----------------//
*************************************************/

using PEUtils;
using UnityEngine;
using System.Collections.Generic;

public class PERoot : UnitySingleton<PERoot> {
    readonly List<ILogic> services = new();
    public TimerSvc timerSvc;
    public ResSvc resSvc;
    public EvtSvc evtSvc;
    public NetSvc netSvc;
    public AudioSvc audioSvc;
    public UISvc uiSvc;

    readonly List<ILogic> systems = new();
    public AcctSys acctSys;
    public StageSys stageSys;

    void Start() {
        LogConfig cfg = new() {
            enableLog = true,
            logPrefix = "",
            enableTime = true,
            logSeparate = ">",
            enableThreadID = true,
            enableTrace = true,
            enableSave = true,
            enableCover = true,
            saveName = "MMOClientPELog.txt",
            loggerEnum = LoggerType.Unity
        };
        PELog.InitSettings(cfg);
        this.Log("GameStart...");

        Application.targetFrameRate = 60;
        DontDestroyOnLoad(this);

        Transform uiRoot = transform.Find("Canvas");
        for (int i = 0; i < uiRoot.childCount; i++) {
            uiRoot.GetChild(i).gameObject.SetActive(false);
        }

        timerSvc = new TimerSvc();
        services.Add(timerSvc);
        resSvc = new ResSvc();
        services.Add(resSvc);
        evtSvc = new EvtSvc();
        services.Add(evtSvc);
        netSvc = new NetSvc();
        services.Add(netSvc);
        audioSvc = new AudioSvc();
        services.Add(audioSvc);
        uiSvc = new UISvc();
        services.Add(uiSvc);
        for (int i = 0; i < services.Count; i++) {
            services[i].Init();
        }

        acctSys = new AcctSys();
        systems.Add(acctSys);
        stageSys = new StageSys();
        systems.Add(stageSys);
        for (int i = 0; i < systems.Count; i++) {
            systems[i].Init();
        }

        fsm.Add(CPlayMode.Login, new LoginMode());
        fsm.Add(CPlayMode.Create, new CreateMode());
        fsm.Add(CPlayMode.Major, new MajorMode());
        fsm.Add(CPlayMode.Wild, new WildMode());

        stageSys.LoadGameStage(1, () => {
            EnterGameMode(CPlayMode.Login);
        });
    }
    void Update() {
        for (int i = 0; i < services.Count; i++) {
            services[i].Tick();
        }
        for (int i = 0; i < systems.Count; i++) {
            systems[i].Tick();
        }
        if (currentMode != CPlayMode.None) {
            fsm[currentMode].Update();
        }
    }
    void OnApplicationQuit() {
        for (int i = services.Count - 1; i >= 0; i--) {
            services[i].UnInit();
        }
    }

    readonly Dictionary<CPlayMode, BaseMode> fsm = new();
    private CPlayMode currentMode = CPlayMode.None;
    public void EnterGameMode(CPlayMode targetMode) {
        if (fsm.ContainsKey(targetMode)) {
            if (currentMode != CPlayMode.None) {
                fsm[currentMode].Exit();
            }
        }
        fsm[targetMode].Enter();
        currentMode = targetMode;
    }
    public void ExitGameMode() {
        if (currentMode != CPlayMode.None) {
            fsm[currentMode].Exit();
            currentMode = CPlayMode.None;
        }
    }
}
